<template>
  <div class="bom-container">
    <!-- 页面标题 -->
    <div class="page-title">
      <h1>BOM管理</h1>
    </div>

    <!-- 操作区域 -->
    <div class="action-section">
      <div class="left-actions">
        <el-button type="primary" class="add-btn" @click="goToAddBOM">
          <el-icon><Plus /></el-icon>
          新增
        </el-button>
        <el-button type="primary" class="import-btn">
          <el-icon><Upload /></el-icon>
          导入
        </el-button>
        <el-button type="primary" class="download-btn">
          <el-icon><Download /></el-icon>
          下载模板
        </el-button>
      </div>
      
      <div class="right-actions">
        <el-input
          v-model="searchKeyword"
          placeholder="请搜索"
          class="search-input"
          clearable
        >
          <template #suffix>
            <el-icon class="search-icon"><Search /></el-icon>
          </template>
        </el-input>
        <el-button 
          type="primary" 
          class="view-btn"
          @click="toggleQueryPanel"
        >
          <el-icon><View /></el-icon>
        </el-button>
      </div>
    </div>

    <!-- 查询条件悬浮面板 -->
    <div v-if="isQueryVisible" class="query-overlay">
      <el-form :model="queryForm" label-width="80px" class="query-form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="BOM编号">
              <el-input v-model="queryForm.bomNumber" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="BOM主题">
              <el-input v-model="queryForm.bomSubject" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="默认BOM">
              <el-select v-model="queryForm.isDefault" placeholder="请选择" clearable>
                <el-option label="是" :value="true" />
                <el-option label="否" :value="false" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本">
              <el-input v-model="queryForm.version" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="成品类型">
              <el-select v-model="queryForm.finishedProductType" placeholder="请选择" clearable>
                <el-option label="成品" value="成品" />
                <el-option label="半成品" value="半成品" />
                <el-option label="其他" value="其他" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成品名称">
              <el-input v-model="queryForm.finishedProductName" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="日产量">
              <div class="range-inputs">
                <el-input v-model="queryForm.dailyOutputMin" placeholder="最小值" />
                <span class="range-sep">~</span>
                <el-input v-model="queryForm.dailyOutputMax" placeholder="最大值" />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>

        <div class="query-actions">
          <el-button @click="closeQueryPanel">关闭</el-button>
          <el-button @click="clearQuery">清空</el-button>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </div>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="table-section">
      <el-table
        :data="bomList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        border
        stripe
        class="bom-table"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="serialNumber" label="序号" width="60" />
        <el-table-column prop="bomNumber" label="BOM编号" width="120" />
        <el-table-column prop="bomSubject" label="BOM主题" width="180" />
        <el-table-column prop="productNumber" label="成品编号" width="120" />
        <el-table-column prop="productName" label="成品名称" width="150" />
        <el-table-column prop="specification" label="规格型号" width="200" />
        <el-table-column prop="unit" label="单位" width="80" />
        <el-table-column prop="brand" label="品牌" width="100" />
        <el-table-column prop="isDefault" label="默认BOM" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.isDefault ? 'success' : 'info'">
              {{ scope.row.isDefault ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="dailyOutput" label="日产量" width="100" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" link size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" link size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
            <el-button type="info" link size="small" @click="handleView(scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-section">
      <div class="pagination-info">
        <span>共{{ total }}项数据</span>
        <el-select v-model="pageSize" size="small" class="page-size-select">
          <el-option label="5条/页" :value="5" />
          <el-option label="10条/页" :value="10" />
          <el-option label="20条/页" :value="20" />
          <el-option label="50条/页" :value="50" />
        </el-select>
      </div>
      
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :total="total"
        layout="prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="pagination-controls"
      />
    </div>


  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Plus, Upload, Download, Search, View } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

// 路由实例
const router = useRouter()

// 响应式数据
const searchKeyword = ref('')
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(101)
const selectedRows = ref<any[]>([])

// 查询面板状态与表单
const isQueryVisible = ref(false)
const queryForm = ref({
  bomNumber: '',
  bomSubject: '',
  isDefault: null as null | boolean,
  finishedProductType: '',
  version: '',
  finishedProductName: '',
  dailyOutputMin: '',
  dailyOutputMax: ''
})

// BOM列表数据 - 按照效果图显示10条数据
const bomList = ref([
  {
    id: 1,
    serialNumber: 1,
    bomNumber: 'BOM00001',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '产品',
    isDefault: true,
    dailyOutput: 50
  },
  {
    id: 2,
    serialNumber: 2,
    bomNumber: 'BOM00012',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '物料',
    isDefault: false,
    dailyOutput: 50
  },
  {
    id: 3,
    serialNumber: 3,
    bomNumber: 'BOM00023',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '产品',
    isDefault: true,
    dailyOutput: 50
  },
  {
    id: 4,
    serialNumber: 4,
    bomNumber: 'BOM00034',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '物料',
    isDefault: false,
    dailyOutput: 50
  },
  {
    id: 5,
    serialNumber: 5,
    bomNumber: 'BOM00045',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '产品',
    isDefault: true,
    dailyOutput: 50
  },
  {
    id: 6,
    serialNumber: 6,
    bomNumber: 'BOM00056',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '物料',
    isDefault: false,
    dailyOutput: 50
  },
  {
    id: 7,
    serialNumber: 7,
    bomNumber: 'BOM00067',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '产品',
    isDefault: true,
    dailyOutput: 50
  },
  {
    id: 8,
    serialNumber: 8,
    bomNumber: 'BOM00078',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '物料',
    isDefault: false,
    dailyOutput: 50
  },
  {
    id: 9,
    serialNumber: 9,
    bomNumber: 'BOM00089',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '产品',
    isDefault: true,
    dailyOutput: 50
  },
  {
    id: 10,
    serialNumber: 10,
    bomNumber: 'BOM00090',
    bomSubject: '超能胶水BOM',
    productNumber: 'CP002',
    productName: '超能胶水',
    specification: 'GL-1000-TIMEBOND',
    unit: '公斤',
    brand: '物料',
    isDefault: false,
    dailyOutput: 50
  }
])

// 方法
const handleSelectionChange = (selection: any[]) => {
  selectedRows.value = selection
}

// 跳转到新增BOM页面
const goToAddBOM = () => {
  router.push('/bom/add')
}

const handleEdit = (row: any) => {
  ElMessage.info(`编辑BOM: ${row.bomNumber}`)
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(
    `确定要删除BOM ${row.bomNumber} 吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleView = (row: any) => {
  ElMessage.info(`查看BOM: ${row.bomNumber}`)
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  // 这里可以调用API重新获取数据
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  // 这里可以调用API重新获取数据
}

// 查询面板方法
const toggleQueryPanel = () => {
  isQueryVisible.value = !isQueryVisible.value
}

const closeQueryPanel = () => {
  isQueryVisible.value = false
}

const clearQuery = () => {
  Object.assign(queryForm.value, {
    bomNumber: '',
    bomSubject: '',
    isDefault: null as null | boolean,
    finishedProductType: '',
    version: '',
    finishedProductName: '',
    dailyOutputMin: '',
    dailyOutputMax: ''
  })
}

const handleQuery = () => {
  ElMessage.info('点击了查询按钮')
  // 这里可以调用API进行实际的查询逻辑
}

// 生命周期
onMounted(() => {
  // 页面加载时的初始化逻辑
})
</script>

<style scoped>
.bom-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.page-title {
  margin-bottom: 20px;
}

.page-title h1 {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.action-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.left-actions {
  display: flex;
  gap: 12px;
}

.left-actions .el-button {
  height: 40px;
  padding: 0 20px;
}

.right-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

.search-input {
  width: 300px;
}

.search-icon {
  cursor: pointer;
  color: #909399;
}

/* 悬浮查询面板 */
.query-overlay {
  position: relative;
  margin-top: 10px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
  padding: 16px;
  z-index: 5;
}

.query-form :deep(.el-form-item) {
  margin-bottom: 12px;
}

.range-inputs {
  display: flex;
  align-items: center;
}

.range-inputs .el-input {
  flex: 1;
}

.range-sep {
  margin: 0 8px;
  color: #909399;
}

.query-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.table-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.bom-table {
  border-radius: 4px;
  overflow: hidden;
}

.pagination-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-info {
  display: flex;
  align-items: center;
  color: #606266;
  font-size: 14px;
}

.page-size-select {
  margin-left: 10px;
  width: 100px;
}

.pagination-controls {
  margin: 0;
}

/* 确保表格内容完整显示 */
:deep(.el-table) {
  font-size: 14px;
}

:deep(.el-table th) {
  background-color: #fafafa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-button--link) {
  padding: 4px 8px;
  font-size: 14px;
}

:deep(.el-tag) {
  font-size: 12px;
  padding: 2px 8px;
}


</style>
